package team.fivez.defaultmanage.dao.Impl;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import team.fivez.defaultmanage.dao.DefaultApplicationDao;
import team.fivez.defaultmanage.domain.DefaultApplication;
import team.fivez.defaultmanage.domain.IndustryInfo;
import team.fivez.defaultmanage.domain.RegionInfo;


import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;

@Repository
public class DefaultApplicationDaoImpl implements DefaultApplicationDao {

    @Resource
    private JdbcTemplate jdbcTemplate;

    //插入申请表
    @Override
    public int insertDefaultApplication(DefaultApplication defaultApplication) {
        String sql="insert into default_application(" +
                "application_number,severity,note,approvement_status,application_time," +
                "applicant_account,customer_name,redo_status) values(?,?,?,?,?,?,?,?)";
        String applicationNumber = defaultApplication.getApplicationNumber();
        String severity = defaultApplication.getSeverity();
        String note = defaultApplication.getNote();
        int approvementStatus=0;
        Timestamp applicationTime = defaultApplication.getApplicationTime();
        String applicantAccount = defaultApplication.getApplicantAccount();
        String customerName = defaultApplication.getCustomerName();
        int redoStatus=0;

        return jdbcTemplate.update(sql,applicationNumber,severity,note,approvementStatus,
                applicationTime,applicantAccount,customerName,redoStatus);
    }

    //根据客户名查找一个违约申请单信息
    @Override
    public DefaultApplication getDefaultApplicationByCustomerName(String customerName) {
        String sql="select * from default_application where customer_name=?";
        return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(DefaultApplication.class),customerName);
    }


    //根据审核状态和申请人查看申请单信息列表
    @Override
    public List<DefaultApplication> getListByApprovementStatusAndApplicantAccount(int approvementStatus, String applicantAccount) {
        String sql="select * from default_application where approvement_status=? and applicant_account=?";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(DefaultApplication.class),approvementStatus,applicantAccount);
    }

    //根据客户名称和申请人查看申请单信息列表
    @Override
    public List<DefaultApplication> getListByCustomerNameAndApplicantAccount(String customerName, String applicantAccount) {
        String sql="select * from default_application where customer_name like ? and applicant_account=?";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(DefaultApplication.class),"%"+customerName+"%",applicantAccount);

    }

    @Override
    public List<DefaultApplication> getAllList() {
        String sql="select * from default_application";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(DefaultApplication.class));
    }


    //根据审核状态查找违约单信息列表
    @Override
    public List<DefaultApplication> getListByApprovementStatus(int approvementStatus) {
        String sql="select * from default_application where approvement_status=?";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(DefaultApplication.class),approvementStatus);
    }

    @Override
    public List<DefaultApplication> getListByApplicantAccount(String applicantAccount) {
        String sql="select * from default_application where applicant_account=?";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(DefaultApplication.class),applicantAccount);

    }

    //根据客户名称查找违约单信息列表
    @Override
    public List<DefaultApplication> getListByCustomerName(String customerName) {
        String sql="select * from default_application where customer_name like ?";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(DefaultApplication.class),"%"+customerName+"%");
    }

    //更改审核状态
    @Override
    public void updateApprovementStatusByApplicationNumber(int approvementStatus,String applicationNumber) {
        String sql="update default_application set approvement_status=? where application_number=?";
        jdbcTemplate.update(sql,approvementStatus,applicationNumber);
    }

    //根据申请单号进行查找
    @Override
    public DefaultApplication getDefaultApplicationByNumber(String number) {
        String sql="select * from default_application where application_number=?";
        return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(DefaultApplication.class),number);
    }

    //更改申请单的重生标记
    @Override
    public void updateRedoStatusByApplicationNumber(String applicationNumber, int status) {
        String sql="update default_application set redo_status=? where application_number=?";
        jdbcTemplate.update(sql,status,applicationNumber);
    }

    //更新审核时间
    @Override
    public void updateApplicationTimeByApplicationNumber(String applicationNumber, Timestamp approvementTime) {
        String sql="update default_application set approvement_time=? where application_number=?";
        jdbcTemplate.update(sql,approvementTime,applicationNumber);

    }

    //更新审核人姓名
    @Override
    public void updateDefaultApproverNameByApplicationNumber(String applicationNumber, String defaultApproverName) {
        String sql="update default_application set default_approver_name=? where application_number=?";
        jdbcTemplate.update(sql,defaultApproverName,applicationNumber);

    }

    //查询申请单有没有该用户的申请单
    @Override
    public Integer selectCountByName(String customerName) {
        String sql="select count(*) from default_application where customer_name=?";
        return jdbcTemplate.queryForObject(sql,Integer.class,customerName);
    }

    @Override
    public List<IndustryInfo> getDefaultIndustryInfo() {
        String sql="select * from industry_default_V";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(IndustryInfo.class));

    }

    @Override
    public List<RegionInfo> getDefaultRegionInfo() {
        String sql="select * from region_default_V";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(RegionInfo.class));
    }


}
